package com.example.mytodo

import android.annotation.SuppressLint
import android.app.Activity
import android.content.ContentValues
import android.content.Intent
import android.database.sqlite.SQLiteDatabase
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_edit_todo.*
import kotlinx.android.synthetic.main.activity_new_todo.*


class EditTodoActivity() :AppCompatActivity() {
    val TAG = "@@EditTodoActivity"

    private var toUpdate:Atodo? = null
    private val helper: DBHelper = DBHelper(this, "todo", 1)


    var idx:Int = 0


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_edit_todo)

        idx=intent.getIntExtra("idx",0)
        setContentView(R.layout.activity_edit_todo)

        val userInputWatcher = object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                Log.i(TAG,"beforeTextChanged:$p0")
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                Log.i(TAG,"onTextChanged:$p0")
            }

            override fun afterTextChanged(p0: Editable?) {
                Log.i(TAG,"afterTextChanged:$p0")
            }

        }

        edittodo_edittext_todo.addTextChangedListener(userInputWatcher)


        edittodo_btn_submit.setOnClickListener {
            saveInput()
            Log.d(TAG, "saved input")

            val intent = Intent(this,MainActivity()::class.java)
            val mainAct : Activity = MainActivity()
            mainAct.finish()

            startActivity(intent)

            finish()

        }
    }

    private fun saveInput() {
        val text = edittodo_edittext_todo.text.toString()
        saveInDb(text)
    }

    private fun saveInDb(text: String) {
        val db = helper.writableDatabase
        val item = Atodo(text,System.currentTimeMillis(),0)
        toUpdate = item
        val values = ContentValues().apply {
            put(Atodo.T_TODOSTRING,item.todoString)
            put(Atodo.T_TIME,item.addtime)
            put(Atodo.T_STATE,item.state)
        }
        var rs = -1
        println("toUpdate :$toUpdate")
        Log.d(TAG,"toUpdate :$toUpdate")
        item.id = idx
        Log.d(TAG,"update row id = $idx")
        rs = db.update(Atodo.TABLE,values,"id = ?", arrayOf(toUpdate?.id.toString()))

        Toast.makeText(this,if(rs < 0) "保存失败" else "保存成功" , Toast.LENGTH_LONG).show()

    }

    @SuppressLint("Range")
    private fun readInDB(): ArrayList<Atodo> {
        val db = helper.writableDatabase
        val cursor = db.query(Atodo.TABLE,null,null,null,null,null,
            "${Atodo.T_ID} desc")
        val arr = arrayListOf<Atodo>()
        if(cursor.moveToFirst()){
            do{
                arr.add(
                    Atodo(
                        cursor.getString(cursor.getColumnIndex(Atodo.T_TODOSTRING)),
                        cursor.getLong(cursor.getColumnIndex(Atodo.T_TIME)),
                        cursor.getInt(cursor.getColumnIndex(Atodo.T_STATE)),

                        ).apply {
                        id = cursor.getColumnIndex(Atodo.T_ID)
                    }
                )
            }while (cursor.moveToNext())
        }

        cursor.close()
        return arr

    }

    fun findIdx(id: Int?): Int {
        var idx = -1
        val data = readInDB()
        data.forEachIndexed { index, todo ->
            if (todo.id == id) {
                idx = index
            }
        }
        return idx
    }




//    override fun onStart() {
//        super.onStart()
//        Log.d(TAG,"onStart")
//    }
//
//    override fun onResume() {
//        super.onResume()
//        Log.d(TAG,"onResume")
//    }
//
//    override fun onPause() {
//        super.onPause()
//        Log.d(TAG,"onPause")
//    }
//
//    override fun onStop() {
//        super.onStop()
//        Log.d(TAG,"onStop")
//    }
//
//
//
//    override fun onDestroy() {
//
//        super.onDestroy()
//        Log.d(TAG,"onDestroy")
//    }



}